package com.ndp.fb.rdb;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ReadPreference;
import com.ndp.fb.config.BaseConfig;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.CommonConstants;
import com.ndp.fb.constant.ConfigConst;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.Arrays;

/**
 * Created by hunk.qin on 2015/1/26.
 */
public class RdbApplication extends BaseConfig {

    @Override
    public ApplicationConfig registerApplicationConfig() {
        return new ApplicationConfig(ConfigConst.RDB_SERVICES_ID);
    }

    @Override
    public String getRegistryGroup() {
        return ConfigConst.DUBBO_REGISTER_GROUP;
    }


    @Bean()
    public ServletRegistrationBean ServletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        StatViewServlet statViewServlet = new StatViewServlet();
        bean.setServlet(statViewServlet);
        bean.setOrder(Integer.MAX_VALUE);
        bean.setLoadOnStartup(Integer.MAX_VALUE);
        bean.setUrlMappings(Arrays.asList("/druid/*"));
        return bean;
    }

    private DruidDataSource dataWriteSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(ConfigCentre.getString(ConfigConst.MYSQL_WRITE_URL));
        dataSource.setUsername(ConfigCentre.getString(ConfigConst.MYSQL_WRITE_USERNAME));
        dataSource.setPassword(ConfigCentre.getString(ConfigConst.MYSQL_WRITE_PASSWORD));

        dataSource.setMaxPoolPreparedStatementPerConnectionSize(ConfigCentre.getInteger(ConfigConst.DRUID_DATASOURCE_MAXPOOLPREPAREDSTATEMENTPERCONNECTIONSIZE));
        dataSource.setInitialSize(ConfigCentre.getInteger(ConfigConst.DRUID_DATASOURCE_INITIALSIZE));
        dataSource.setMinIdle(ConfigCentre.getInteger(ConfigConst.DRUID_DATASOURCE_MINIDLE));
        dataSource.setMaxActive(ConfigCentre.getInteger(ConfigConst.DRUID_DATASOURCE_MAXACTIVE));
        dataSource.setMaxWait(ConfigCentre.getLong(ConfigConst.DRUID_DATASOURCE_MAXWAIT));


        dataSource.setDriverClassName(CommonConstants.MYSQL_DRIVER);
        dataSource.setTestOnBorrow(CommonConstants.DRUID_TESTONBORROW);
        dataSource.setTestOnReturn(CommonConstants.DRUID_TESTONRETURN);
        dataSource.setTestWhileIdle(CommonConstants.DRUID_TESTWHILEIDLE);
        dataSource.setValidationQuery(CommonConstants.DRUID_VALIDATIONQUERY);
        dataSource.setTimeBetweenEvictionRunsMillis(CommonConstants.DRUID_TIMEBETWEENEVICTIONRUNSMILLIS);
        dataSource.setMinEvictableIdleTimeMillis(CommonConstants.DRUID_MINEVICTABLEIDLETIMEMILLIS);
        dataSource.setPoolPreparedStatements(CommonConstants.DRUID_POOLPREPAREDSTATEMENTS);
        dataSource.setFilters(CommonConstants.DRUID_FILTERS);
        dataSource.init();
        return dataSource;
    }

    @Bean(name = "jdbcWriteTemplate")
    public JdbcTemplate jdbcWriteTemplate()throws Exception {
        return new JdbcTemplate(dataWriteSource());
    }

    @Bean
    public DB mongoDBClient() throws Exception {
		MongoClientURI connectionString = new MongoClientURI(ConfigCentre.getString(ConfigConst.MONGODB_HOST));
		MongoClient mongoClient = new MongoClient(connectionString);
        DB db = mongoClient.getDB(ConfigCentre.getString(ConfigConst.MONGODB_NAME));
        db.setReadPreference(ReadPreference.secondaryPreferred());
        return db;
    }

    @Bean
    public DB mongoDBClientPrimary() throws Exception {
		MongoClientURI connectionString = new MongoClientURI(ConfigCentre.getString(ConfigConst.MONGODB_HOST));
		MongoClient mongoClient = new MongoClient(connectionString);
        DB db = mongoClient.getDB(ConfigCentre.getString(ConfigConst.MONGODB_NAME));
        db.setReadPreference(ReadPreference.primary());
        return db;
    }
    @Bean
    public DB adResourceMongoDBClient() throws Exception {
		MongoClientURI connectionString = new MongoClientURI(ConfigCentre.getString(ConfigConst.ADRESOURCE_MONGODB_HOST));
		MongoClient mongoClient = new MongoClient(connectionString);
        DB db = mongoClient.getDB(ConfigCentre.getString(ConfigConst.ADRESOURCE_MONGODB_NAME));
        db.setReadPreference(ReadPreference.secondaryPreferred());
        return db;
    }

    @Bean
    public DB adResourceMongoDBClientPrimary() throws Exception {
		MongoClientURI connectionString = new MongoClientURI(ConfigCentre.getString(ConfigConst.ADRESOURCE_MONGODB_HOST));
		MongoClient mongoClient = new MongoClient(connectionString);
        DB db = mongoClient.getDB(ConfigCentre.getString(ConfigConst.ADRESOURCE_MONGODB_NAME));
        db.setReadPreference(ReadPreference.primary());
        return db;
    }
    @Bean
    public DB adHistoryDBClient() throws Exception {
		MongoClientURI connectionString = new MongoClientURI(ConfigCentre.getString(ConfigConst.ADHISTORY_MONGODB_HOST));
		MongoClient mongoClient = new MongoClient(connectionString);
        DB db = mongoClient.getDB(ConfigCentre.getString(ConfigConst.ADHISTORY_MONGODB_NAME));
        db.setReadPreference(ReadPreference.secondaryPreferred());
        return db;
    }
    @Bean
    public DB pixelDBClient() throws Exception {
		MongoClientURI connectionString = new MongoClientURI(ConfigCentre.getString(ConfigConst.PIXEL_MONGODB_HOST));
		MongoClient mongoClient = new MongoClient(connectionString);
        DB db = mongoClient.getDB(ConfigCentre.getString(ConfigConst.PIXEL_MONGODB_NAME));
        db.setReadPreference(ReadPreference.primary());
        return db;
    }
}
